package com.cicc.beergamevirtualsimulationsystem.controller;


import com.cicc.beergamevirtualsimulationsystem.entity.UserEntity;
import com.cicc.beergamevirtualsimulationsystem.model.R;
import com.cicc.beergamevirtualsimulationsystem.model.user.dto.LoginDto;
import com.cicc.beergamevirtualsimulationsystem.model.user.dto.UserDto;
import com.cicc.beergamevirtualsimulationsystem.service.LoginService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

/**
 * 用户登录模块
 */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class LoginController {

    private final LoginService loginService;

    /**
     * 发送手机短信验证码【注册】
     *
     * @param phone
     * @return
     */
    @GetMapping("/phoneCaptcha")
    public R phoneCaptcha(HttpServletRequest request, @RequestParam("phone")String phone){
        loginService.phoneCaptcha(request,phone);
        return  R.ok();
    }

    /**
     *登录
     *
     * @param loginDto
     * @return
     */

    @PostMapping("/login")
    public R login(@Valid @RequestBody LoginDto loginDto){
        return loginService.login(loginDto);
    }

    /**
     *注册
     *
     * @param userDto
     * @return
     */
    @PostMapping("/reg")
    public R<String> reg(HttpServletRequest request, @Valid @RequestBody UserEntity userDto){
        // 获取请求的ip

        return R.ok(loginService.reg(request,userDto));
    }

    /**
     * 发送验证码【忘记密码】
     * @param phone
     * @return
     */
    @GetMapping("/updateCaptcha")
    public R updateCaptcha(HttpServletRequest request,@RequestParam("phone")String phone){
        loginService.updateCaptcha(request,phone);
        return R.ok("验证码发送成功");
    }

    /**
     * 修改密码
     * @param userDto
     * @return
     */
    @PutMapping("/updatePwd")
    public R updatePwd(@Valid @RequestBody UserDto userDto){
        loginService.updatePwd(userDto);
        return R.ok("修改成功");
    }



}
